<html>
<head><meta charset="utf-8"><title>Type punning constants to atomics... carefully · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html">Type punning constants to atomics... carefully</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206894539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206894539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206894539">(Aug 14 2020 at 03:09)</a>:</h4>
<p>I have a type like </p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Inner</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">is_static</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">count</span>: <span class="nc">AtomicUsize</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">payload</span>: <span class="nc">Payload</span><span class="p">,</span><span class="w"> </span><span class="c1">// not important but constant, read only, POD, align 1, boring.</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>. This type is essentially always behind a pointer like <code>#[repr(transparent)] struct Outer(NonNull&lt;Inner&gt;);</code>.<br>
The tricky part is that I allow defining const instances of Inner/Outer by using</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">StaticInner</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">is_static</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"> </span><span class="c1">// true iff defined as a static constant</span>
<span class="w">    </span><span class="n">count</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">payload</span>: <span class="nc">Payload</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>That is, <code>StaticInner</code> is the same thing as <code>Inner</code> but <code>count</code> is a usize. All <code>StaticInner</code> instances have <code>is_static: true</code>, and all others have it false (in reality Inner and StaticInner are the same struct with different generic params to avoid getting out of sync). Then to define a the statics, I do this.</p>
<div class="codehilite"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">SI</span>: <span class="kp">&amp;</span><span class="nc">StaticInner</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">StaticInner</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">is_static</span>: <span class="nc">true</span><span class="p">,</span><span class="w"> </span><span class="n">count</span>: <span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">payload</span>: <span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="n">EXAMPLE</span>: <span class="nc">Outer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="n">Outer</span><span class="p">(</span><span class="n">NonNull</span>::<span class="n">new_unchecked</span><span class="p">(</span><span class="n">SI</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">Inner</span><span class="p">))</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div>


<p>So, this requires quite a bit of care to avoid UB, but I'm pretty sure I've managed it, but wanted to run it by y'all.</p>
<p>(TLDR: if there's any chance the pointer I'm working with is to one of these type-punned StaticInners, I not only avoid touching <code>count</code> in any way at all, I avoid direct field access to any of the other fields, and instead use pointer arithmetic + memoffset::offset_of + casting. If i do have an <code>&amp;Inner</code>, it's only after verifying that it is not a <code>StaticInner</code>. Potentially worth noting that static or not, these values are fully immutable — that <code>count</code> is the mutability, interior or otherwise).</p>
<p>Anyway, now, obviously it's UB if i were to write to a <code>Inner::count</code> where it was actually a type_punned <code>StaticInner</code>. Less obviously, it's still UB if I make a <code>&amp;AtomicUsize</code> without verifying that it's not a <code>StaticInner</code>. That means I can't read from <code>Inner::count</code>. It's also almost certainly UB if I were to ever view a non-static <code>Inner</code> as a <code>StaticInner</code>, but I have no need for this.</p>
<p>More tricky, but also the reason I'm pretty confident it's fine, is that I never allow an <code>&amp;Inner</code> to exist without first verifying that it's not static. That means I only rarely use direct field access (the places where I do are always after I've verified <code>is_static</code> is <code>true</code>).</p>
<p>So usually to get the payload i'll do <code>&amp;*ptr.cast::&lt;u8&gt;().add(offset_of!(Inner, payload)).cast::&lt;Payload&gt;()</code>, and to test <code>is_static</code> I'll do <code>*ptr.cast::&lt;u8&gt;().add(offset_of!(Inner, is_static)).cast::&lt;bool&gt;()</code> (FWIW I was worried that it wouldn't compile to a constant but it seems like it does on release at least).</p>
<p>My paranoia in all of this is from the assumption that if I have a &amp;T, not only must it be valid, but all of its fields must be valid as well, and also the assumption that a <code>AtomicUsize</code> would be invalid if it actually lives in non-writable memory.</p>
<p>So, given this, I can't really see how I'd be causing be UB here, since rust doesn't care what is actually behind a <code>*const Inner</code>, and all <code>&amp;T</code>s are fully of the type they claim. I did want to make sure I wasn't missing something though — My first pass at this actually used field accesses and while miri didn't mind, I'm pretty sure it was still UB.</p>



<a name="206894894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206894894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206894894">(Aug 14 2020 at 03:20)</a>:</h4>
<p>The use case here a (non-generic, don't worry) <code>Arc&lt;T&gt;</code> that can also hold constant data.</p>



<a name="206897731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206897731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206897731">(Aug 14 2020 at 04:40)</a>:</h4>
<p>Couldn't you just use:</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Inner</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">count</span>: <span class="nc">Count</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">payload</span>: <span class="nc">Payload</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[repr(C, u8)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">Count</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Atomic</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">count</span>: <span class="nc">AtomicUsize</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="n">Static</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">count</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">},</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="n">SI</span>: <span class="kp">&amp;</span><span class="nc">Inner</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Inner</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">count</span>: <span class="nc">Count</span>::<span class="n">Static</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span><span class="w"> </span><span class="n">payload</span>: <span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="n">EXAMPLE</span>: <span class="nc">Outer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Outer</span><span class="p">(</span><span class="n">NonNull</span>::<span class="n">from</span><span class="p">(</span><span class="n">SI</span><span class="p">));</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">new_outer</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Outer</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">inner</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">Inner</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">count</span>: <span class="nc">Count</span>::<span class="n">Atomic</span><span class="p">(</span><span class="mi">0</span><span class="k">usize</span><span class="p">.</span><span class="n">into</span><span class="p">()),</span><span class="w"> </span><span class="n">payload</span>: <span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="n">Outer</span><span class="p">(</span><span class="n">NonNull</span>::<span class="n">from</span><span class="p">(</span><span class="n">inner</span><span class="p">.</span><span class="n">leak</span><span class="p">()))</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="206899391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206899391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206899391">(Aug 14 2020 at 05:25)</a>:</h4>
<p>Yeah, but the constant instances dont put in read-only memory if i do that tho. I could probably live with that if what I did is actually unsound though.</p>
<p>That said, in truth, another reason is that I didn't think of it. I did try Option&lt;AtomicUsize&gt; but could have sworn that it wouldn't let me put it in a <code>const</code> (this is of course, not true, I was mistaken). Oh well. It being in rodata is pretty nice for a few reasons for me though, so I'd rather not give that up after having spent the effort to make it happen.</p>
<p>(It's also a pretty small module with very high test coverage ratio, so I'm not too worried about it being harder to maintain because of having done that...)</p>



<a name="206902484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206902484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206902484">(Aug 14 2020 at 06:33)</a>:</h4>
<blockquote>
<p>Less obviously, it's still UB if I make a &amp;AtomicUsize without verifying that it's not a StaticInner. </p>
</blockquote>
<p>Why that? Hm, I guess Stacked Borrows does make this UB, but I am not sure there's any good reason for that.</p>



<a name="206904041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206904041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206904041">(Aug 14 2020 at 07:07)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully/near/206899391">said</a>:</p>
<blockquote>
<p>Yeah, but the constant instances dont put in read-only memory if i do that tho. I could probably live with that if what I did is actually unsound though.</p>
</blockquote>
<p>You could probably use <code>#[link_section = ".ro_data"]</code> to work around that. I wouldn't expect the <code>enum</code> version to be unsound if the actual <code>static</code>'s value (rather than the type) didn't contain any interior mutability.</p>



<a name="206904490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206904490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206904490">(Aug 14 2020 at 07:16)</a>:</h4>
<p><span class="user-mention" data-user-id="229517">@Jacob Lifshay</span> would way rather it be automatic than manually configuring link_sections. Not every executable format calls it rodata either. This seems like it would be adding something much more dubious to be honest.</p>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Well, miri didn't like it for one thing lol, and I figured that probably means a theres a requirement for any <code>&amp;T</code> where <code>T</code> holds an <code>UnsafeCell</code> is that it be writable memory. That was my intuition. I'd certainly prefer if this <em>not</em> be the requirement, but it's also not that bad.</p>



<a name="206904834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206904834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206904834">(Aug 14 2020 at 07:23)</a>:</h4>
<p><span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> I opened <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/246">an issue</a> for this.</p>



<a name="206904962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206904962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206904962">(Aug 14 2020 at 07:25)</a>:</h4>
<p>glad i mentioned something then</p>



<a name="206905551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206905551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206905551">(Aug 14 2020 at 07:36)</a>:</h4>
<p>yes, thanks for bringing this up :)</p>



<a name="206905589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206905589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206905589">(Aug 14 2020 at 07:37)</a>:</h4>
<p>it's also possible I am just not awake yet to see the problems with permitting such code. but intuitively it shouldn't be needed... feels a bit like casting to a raw pointer, where we also want the new ptr to inherit the permissions of the old one, instead of going by the type of the raw ptr.</p>



<a name="206911432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206911432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206911432">(Aug 14 2020 at 09:21)</a>:</h4>
<p>it doesn't really matter that much for my use case. i guess it might mean i could go back to direct field access and remove the memoffset dep, but i could remove that dep anyway by hardcoding the offsets, so i don't care that much.</p>
<p>Having said that I do feel like I can imagine situations where it turns out I want to use an atomic load to read from something that it wouldn't be valid to write to... But thinking more, these situations actually are mostly better served by operaitons on <code>*const AtomicFoo</code> rather than by turning &amp;Foo -&gt; &amp;AtomicFoo. After all, either: Foo has interior mutability, in which case we're allowed to make it atomic today, or Foo doesn't, in which case &amp;Foo shouldn't be changing, and I shouldn't need atomic operations....</p>
<p>That said, if theres no reason to disallow it, I guess it should be allowed, since it's kind of annoying to manually track if reading from the atomic is legal</p>



<a name="206911541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206911541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206911541">(Aug 14 2020 at 09:22)</a>:</h4>
<blockquote>
<p>That said, if theres no reason to disallow it, I guess it should be allowed</p>
</blockquote>
<p>That is mostly my argument here, yes.</p>



<a name="206911578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206911578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206911578">(Aug 14 2020 at 09:23)</a>:</h4>
<p>All this offset_of, you just need it because <a href="https://github.com/rust-lang/rust/issues/73394">https://github.com/rust-lang/rust/issues/73394</a> is not stable yet, right?</p>



<a name="206937061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206937061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206937061">(Aug 14 2020 at 14:52)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Yeah, that would be nice. In general, it would be really nice tbh — I feel like many times I haven't really been able to find a well defined workaround without it...</p>
<p>That said, for this I could just hard-code the offsets because it's repr(C) and they're simple to compute, but this is not really very robust against changes... although I have good coverage and <code>cross</code> CI on a lot of platforms now... So I'm probably not likely to mess it up without noticing.</p>
<p>But yeah, literally the thing I am doing is essentially <code>raw_const</code> so it would be great if they stabilized.</p>



<a name="206939474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20constants%20to%20atomics...%20carefully/near/206939474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20constants.20to.20atomics.2E.2E.2E.20carefully.html#206939474">(Aug 14 2020 at 15:11)</a>:</h4>
<p>stabilization is <a href="https://github.com/rust-lang/rust/issues/73394#issuecomment-664554197">on its way</a> :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>